﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;

namespace Yuyi.PluginForJS.Addin
{
    public class OutlineService
    {
        private DTE _dte;
        private Document _doc;

        private CodeService service;
        public OutlineService(DTE dte,Document doc)
        {
            this._dte = dte;
            this._doc = doc??dte.ActiveDocument;

            service = new CodeService(dte, doc);
        }
        public TextSelection Selection
        {
            get
            {
                if (_doc == null)
                    return null;
                else
                {
                    TextDocument textDocument = (TextDocument)_doc.Object("TextDocument");
                    return textDocument.Selection;
                }
            }
        }
        public void Outline()
        {
            string Code = service.LoadCode();
            if (Selection == null || Code == null || Code.Length <= 0) return;
            Selection.StartOfDocument(false);
            FoldManager foldManager = new FoldManager(Code);
            IList<FoldMarker> folderMarkers = foldManager.GetFoldMarkers();
            foreach (FoldMarker foldMarker in folderMarkers)
            {
                Selection.MoveTo(foldMarker.EndLine, foldMarker.EndColumn + 1, true);
                Selection.SwapAnchor();
                Selection.MoveToLineAndOffset(foldMarker.StartLine, foldMarker.StartColumn, true);
                Selection.OutlineSection();
            }
        }
    }
}
